package com.ms.p2p.controller.admin;


import com.alibaba.dubbo.config.annotation.Reference;
import com.ms.common.utils.MsDateUtils;
import com.ms.common.utils.PageData;
import com.ms.p2p.bean.PageBean;
import com.ms.p2p.controller.BaseController;
import com.ms.p2p.entity.Page;
import com.ms.p2p.entity.content.Article;
import com.ms.p2p.entity.content.ArticleCategory;
import com.ms.p2p.entity.system.AdminUser;
import com.ms.p2p.plugin.validation.annotation.PermissionValidator;
import com.ms.p2p.plugin.validation.annotation.Validations;
import com.ms.p2p.provider.ArticleCategoryProvider;
import com.ms.p2p.provider.ArticleProvider;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.beans.IntrospectionException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;

/***
 * 新闻、公告
 */
@Controller("AdminArticleController")
@RequestMapping("/admin/article")
public class AdminArticleController extends AdminBaseController {
	boolean binderFlag = false;
	@Reference
	private ArticleProvider articleProvider;
	@Reference
	private ArticleCategoryProvider articleCategoryProvider;

	@Validations(perms = { @PermissionValidator(permission = "news:add") })
	@RequestMapping("/add")
	public String add(HttpServletRequest request) {
		String id = request.getParameter("id");
		List<ArticleCategory> newstype = articleCategoryProvider.getlist();
		// 获取类别列表
		request.setAttribute("newstypelist", newstype);
		if (id != null && !("").equals(id)) {
			Article newsNotices = articleProvider.get(id);
			request.setAttribute("newsNotices", newsNotices);
		}
		return "admin/article/edit";
	}



	@Validations(perms = { @PermissionValidator(permission = "news:edit") })
	@RequestMapping("/edit")
	public String edit(
			@ModelAttribute Article article,
			HttpServletRequest req, HttpSession session)
			throws IntrospectionException, IllegalAccessException,
			InvocationTargetException {
		AdminUser emp = getCurrentEmp();
		article.setUpdateTime(MsDateUtils.getTime());
		article.setUpdatePerson(emp.getName());
		if(article.getId()==null){
			article.setIssueDate(MsDateUtils.getTime());
			article.setOrderid("0");
			articleProvider.insert(article);
		}else{
			articleProvider.update(article);
		}
		// 返回提示页面
		String url = "admin/article/list.aspx";
		req.setAttribute("message", "操作成功");
		req.setAttribute("url", url);
		return "admin/system/message";
	}

	@Validations(perms = { @PermissionValidator(permission = "news:del") })
	@RequestMapping("/delete")
	public String delete(HttpServletRequest request,
			HttpSession session) {
		int id = Integer.parseInt(request.getParameter("id"));
		articleProvider.delete(id);
		String url = "admin/article/list.aspx";
		request.setAttribute("message", "删除成功");
		request.setAttribute("url", url);
		return "admin/system/message";
	}
	@RequestMapping("/list")
	public String newsList() {

		return "admin/article/list";
	}


	@RequestMapping(value = {"/queryData"})
	@ResponseBody
	public Page queryData(Page pages, String title, String status, String type,
							  String issueDate_begin, String issueDate_end,
							  PageBean page, Model model
	) {


		PageData pd = this.getPageData();
		pages.setPd(pd);
		pages = articleProvider.listAll(pages);	//列出Log列表
		return createJSONData(pages);

	}

	@Override
	public boolean getBinderFlag() {
		return false;
	}
}
